///////////////
// config参数：
// value: true/false ,selected/unselected,状态值
// boxType: checkbox/radiobox , squre/circle style, 选框类型，盒子打钩/圆圈。分别适用于多选和单选
// text: text content ， 描述文本
// disabled: true/false, 禁用/使用，禁用后无法不可点击
// style: 样式对象，遵循jQuery样式对象
// 使用方法：
// {
//     type: "checkbox",
//     value: true,
//     text: "单选",
//     pos: "left",//可选
//     boxStyle: "checkbox"
// }
//////////////

define(["utils","base"],function(utils,baseClass){

    var Component = function(config){
        var _this = this;
        config.style = config.style||{};
        this.value = config.value;
        this.boxType = config.boxType || "checkbox";
        config.disabledClassName = config.disabledClassName || "yy-"+this.boxType+"-disabled";
        Component.baseConstructor.call(this,config);
        var className = "yy-"+ this.boxType +" yy-unselect";
        this.checkbox = $("<i class='yy-fontawesome'></i>");
        this.textDom = $("<span>"+(this.config.text||"")+"</span>");
        if(config.textPos ==="left"){
          className+= " yy-"+ this.boxType +"-textleft";
          this.$el.append(this.textDom);
          this.$el.append(this.checkbox);
        }else{
          className+= " yy-"+ this.boxType +"-textright";
          this.$el.append(this.checkbox);
          this.$el.append(this.textDom);
        }
        if(config.textStyle){
            utils.css(this.textDom,config.textStyle);
        }
        this.$el.addClass(className);

        this.setValue(this.value);
        this.$el.bind("click",function(){
          !_this.disabled &&_this.change();
        });

        var changeMethodName = this.config.comKey+"_change";
        this.changeMethod = this.pageview.plugin[changeMethodName];
    }

    utils.extendClass(Component,baseClass);

    Component.prototype.change = function(){
      if(this.changeMethod){
        this.changeMethod.call(this.pageview.plugin,this,{value:!this.value});
      }
    };
    Component.prototype.selected = function(isFromParent){
      if(isFromParent){
        if(this.config.disableParentSelect){
          return;
        }
      }
      this.value = true;
      this.$el.addClass((this.config.selectedClassName||"")+" yy-"+ this.boxType +"-selected");
    };

    Component.prototype.unSelected = function(isFromParent){
      if(isFromParent){
        if(this.config.disableParentSelect){
          return;
        }
      }
      this.value = false;
      this.$el.removeClass((this.config.selectedClassName||"")+" yy-"+ this.boxType +"-selected");
    };
    Component.prototype.setText = function(text){
      this.textDom.html(text);
    };
    Component.prototype.setValue = function(val) {
      this.value = val;
      if(val){
        this.$el.addClass("yy-"+ this.boxType +"-selected");
      }else{
        this.$el.removeClass("yy-"+ this.boxType +"-selected");
      }
    }
    return Component;
});
